package com.example.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

@Component
public class PreFilter extends ZuulFilter {
    @Override
    public String filterType() {//过滤类型
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {//同类型过滤器得排序  优先级
        return 0;
    }

    @Override
    public boolean shouldFilter() {//过滤器是否起作用
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //获取servlet容器
        RequestContext requestContext = RequestContext.getCurrentContext();
        //获取request请求对象
        HttpServletRequest request = requestContext.getRequest();

        //得到头部信息token
        String token = request.getHeader("token" );
//        if(token == null || token.equals("")){
//            //通知过滤器后面得业务不需要执行，到此结束
//            requestContext.setSendZuulResponse(false);
//            //返回错误码
//            requestContext.setResponseStatusCode(401);
//            //返回结果
//            requestContext.setResponseBody("{\"msg\":\"401,  access without permission, plase login  first\"}");
//        }
        return null;
    }
}
